package cn.brilliantfuture.commentry.util;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

/**
 * 
 * Title: Cabinet Platform
 * 
 * Author:  LiTian  
 *
 * Date: 2012-11-14
 * 
 * Description: 条形码生成器
 * 
 */
public class BarcodeMaker {
	
	private static Logger logger = Logger.getLogger(BarcodeMaker.class);

	/**
	 * 生成Code128条形码及密码 v1.1
	 * @param groupSn 供应商编号
	 * @param lockersSn 配送柜编号
	 * @param storeMode 存储模式
	 * @param dataModel 温控模型(如非温控存储方式值为0)
	 * @param serialNum 流水号
	 * @param memberCode 会员编号
	 * @return JSONObject
	 */
	@Deprecated
	public static JSONObject getCode128(String groupSn, String lockersSn, int storeMode, int dataModel, int serialNum, String memberCode){			
			StringBuffer rfidSn = new StringBuffer();
			
			if (null == groupSn || "".equals(groupSn) || groupSn.length() != 3){
				logger.info("Group sn is null!");
				return null;
			}
			if (null == lockersSn || "".equals(lockersSn) || lockersSn.length() != 4){
				logger.info("Lockers sn is null!");
				return null;
			}
			
			if (serialNum < 1 || serialNum > 9999){
				logger.info("Serial number is error! 1 ~ 9999");
				return null;
			}
			
			if (null == memberCode || "".equals(memberCode) || memberCode.length() != 9){
				logger.info("Member code is null!");
				return null;
			}
			
			rfidSn.append(groupSn).append(lockersSn);
			
			if (OrderConstant.GENERAL_STORE_TYPE == storeMode || OrderConstant.INSULATION_STORE_TYPE == storeMode){
				rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(storeMode), 2));
			} else if (OrderConstant.THERMOSTATIC_STORE_TYPE <= storeMode){
				rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(dataModel), 2));
			} else {
				rfidSn.append("01");
			}
			
			// 获取年份和当前天数
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());

			rfidSn.append(DateTimeHelper.dateTimeToStr(calendar.getTime(), "yy"));
			rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(calendar.get(Calendar.DAY_OF_YEAR)), 3));
			
			//流水号
			rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(serialNum), 4));
			
			rfidSn.append(memberCode);
			
			logger.info("Code 128        -> " + rfidSn.toString());
			String checkCode = CRC8.getCRC(rfidSn.toString());
			rfidSn.append(CommEntryUtil.transferCheckBit(CommEntryUtil.getFormatStr(checkCode, 2)));
			logger.info("Code 128 + CRC8 -> " + rfidSn.toString());
			
			StringBuffer sb = new StringBuffer();
			
			String data = CommEntryUtil.bernstein(rfidSn.toString());
			data = data.substring(28, 45);
			sb.append(data.charAt(0));
			sb.append(data.charAt(9));
			sb.append(data.charAt(5));
			sb.append(CommEntryUtil.dayOfWeek());
			sb.append(data.charAt(3));
			sb.append(data.charAt(14));
			String passwd = CommEntryUtil.confuse(sb.toString());
			
			JSONObject result = new JSONObject();
			
			result.accumulate("rfidSn", rfidSn.toString());
			result.accumulate("passwd", passwd);
			
			return result;
	}
	
	/**
	 * 生成Code128条形码及密码 v1.2
	 * @param groupSn 供应商编号
	 * @param lockersSn 配送柜编号
	 * @param storeMode 存储模式
	 * @param dataModel 温控模型(如非温控存储方式值为0)
	 * @param serialNum 流水号
	 * @return JSONObject
	 */
	@Deprecated
	public static JSONObject getCode128v1DOT2(String groupSn, String lockersSn, int storeMode, int dataModel, int serialNum){

		StringBuffer rfidSn = new StringBuffer();
		
		if (null == groupSn || "".equals(groupSn) || groupSn.length() != 3){
			logger.info("Group sn is null!");
			return null;
		}
		
		if (null == lockersSn || "".equals(lockersSn) || lockersSn.length() != 4){
			logger.info("Lockers sn is null!");
			return null;
		}
		
		if (serialNum < 1 || serialNum > 9999){
			logger.info("Serial number is error! 1 ~ 9999");
			return null;
		}
		
		rfidSn.append(groupSn);
		
		if (OrderConstant.GENERAL_STORE_TYPE == storeMode || OrderConstant.INSULATION_STORE_TYPE == storeMode){
			rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(storeMode), 2));
		} else if (OrderConstant.THERMOSTATIC_STORE_TYPE <= storeMode){
			rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(dataModel), 2));
		} else {
			rfidSn.append("01");
		}
		
		// 获取年份和当前天数
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());

		rfidSn.append(DateTimeHelper.dateTimeToStr(calendar.getTime(), "yy"));
		rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(calendar.get(Calendar.DAY_OF_YEAR)), 3));
		
		//流水号
		rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(serialNum), 4));
		
		logger.info("Code 128        -> " + rfidSn.toString());
		String checkCode = CRC8.getCRC(rfidSn.toString(), Integer.parseInt(lockersSn));
		rfidSn.append(CommEntryUtil.transferCheckBit(CommEntryUtil.getFormatStr(checkCode, 2)));
		logger.info("Code 128 + CRC8 -> " + rfidSn.toString());
		
		StringBuffer sb = new StringBuffer();
		
		String data = getHashAlgorithm(rfidSn.toString(), 333);
		sb.append(data.charAt(3));
		sb.append(data.charAt(38));
		sb.append(data.charAt(17));
		sb.append(CommEntryUtil.dayOfWeek());
		sb.append(data.charAt(23));
		sb.append(data.charAt(14));
		String passwd = CommEntryUtil.confuse(sb.toString());

		JSONObject result = new JSONObject();
		
		result.accumulate("rfidSn", rfidSn.toString());
		result.accumulate("passwd", passwd);
		
		return result;
	}
	
	/**
	 * 生成Code128条形码及密码 v1.2
	 * @param groupSn 供应商编号
	 * @param lockersSn 配送柜编号
	 * @param storeMode 存储模式
	 * @param dataModel 温控模型(如非温控存储方式值为0)
	 * @param gridNum 配送仓编号
	 * @param serialNum 流水号
	 * @param deliverTime 配送时间
	 * @param memberCode 会员编号
	 * @return JSONObject
	 */
	public static JSONObject getCode128v1DOT3(String groupSn, String lockersSn, int storeMode, int dataModel, int gridNum, int serialNum, Date deliverTime, String memberCode){

		StringBuffer rfidSn = new StringBuffer();
		
		if (null == groupSn || "".equals(groupSn) || groupSn.length() != 3){
			logger.info("Group sn is null!");
			return null;
		}
		if (null == lockersSn || "".equals(lockersSn) || lockersSn.length() != 4){
			logger.info("Lockers sn is null!");
			return null;
		}
		
		if (serialNum < 1 || serialNum > 9999){
			logger.info("Serial number is error! 1 ~ 9999");
			return null;
		}
		
		if (null == memberCode || "".equals(memberCode) || memberCode.length() != 9){
			logger.info("Member code is null!");
			return null;
		}
		
		rfidSn.append(groupSn).append(lockersSn);
		
		if (OrderConstant.GENERAL_STORE_TYPE == storeMode || OrderConstant.INSULATION_STORE_TYPE == storeMode){
			rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(storeMode), 2));
		} else if (OrderConstant.THERMOSTATIC_STORE_TYPE <= storeMode){
			rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(dataModel), 2));
		} else {
			rfidSn.append("01");
		}
		
		// 获取年份和当前天数
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(deliverTime);

		rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(gridNum), 2));
		rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(calendar.get(Calendar.DAY_OF_YEAR)), 3));

		rfidSn.append(CommEntryUtil.getFormatStr(String.valueOf(serialNum), 4));
		
		rfidSn.append(memberCode);
		
		logger.info("Code 128        -> " + rfidSn.toString());
		String checkCode = CRC8.getCRC(rfidSn.toString());
		rfidSn.append(CommEntryUtil.transferCheckBit(CommEntryUtil.getFormatStr(checkCode, 2)));
		logger.info("Code 128 + CRC8 -> " + rfidSn.toString());
		
		StringBuffer sb = new StringBuffer();
		
		String data = CommEntryUtil.bernstein(rfidSn.toString());
		data = data.substring(28, 45);
		sb.append(data.charAt(0));
		sb.append(data.charAt(9));
		sb.append(data.charAt(5));
		sb.append(calendar.get(Calendar.DAY_OF_WEEK));
		sb.append(data.charAt(3));
		sb.append(data.charAt(14));
		String passwd = CommEntryUtil.confuse(sb.toString());
		
		JSONObject result = new JSONObject();

		result = new JSONObject();
		result.accumulate("rfidSn", rfidSn.toString());
		result.accumulate("passwd", passwd);

		return result;
	}
	
	/**
	 * Hash计算
	 * @param source 源数据
	 * @param genPoly 根数据
	 * @return String
	 */
	private static String getHashAlgorithm(String source, int genPoly) {
		BigDecimal hash = new BigDecimal(0);
		BigDecimal constant = new BigDecimal(genPoly);

		for (int i = source.length() - 1; i >= 0; i--)
			hash = constant.multiply(hash).add(new BigDecimal(source.charAt(i)));
		return hash.toString();
	}
	
	public static void main(String[] args) {
		
		Calendar calendar = Calendar.getInstance();
		
		System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
	}
}
